package com.nic.view.util;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间、日期工具类
 * Created by Administrator on 2018/11/2.
 */

public class DateTimeUtil {
    static SimpleDateFormat format;

    /**
     * 日期格式：yyyy-MM-dd HH:mm:ss
     **/
    public static final String DF_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /**
     * 日期格式：yyyy-MM-dd HH:mm
     **/
    public static final String DF_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

    /**
     * 日期格式：yyyy-MM-dd
     **/
    public static final String DF_YYYY_MM_DD = "yyyy-MM-dd";

    /**
     * 日期格式：HH:mm:ss
     **/
    public static final String DF_HH_MM_SS = "HH:mm:ss";

    /**
     * 日期格式：HH:mm
     **/
    public static final String DF_HH_MM = "HH:mm";

    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年


    public DateTimeUtil() {

    }

    /**
     * 将日期格式化成友好的字符串：几分钟前、几小时前、几天前、几月前、几年前、刚刚
     *
     * @param date
     * @return
     */
    public static String formatFriendly(Date date) {
        if (date == null) {
            return null;
        }
        long diff = new Date().getTime() - date.getTime();
        long r = 0;
        if (diff > year) {
            r = (diff / year);
            return r + "年前";
        }
        if (diff > month) {
            r = (diff / month);
            return r + "个月前";
        }
        if (diff > day) {
            r = (diff / day);
            return r + "天前";
        }
        if (diff > hour) {
            r = (diff / hour);
            return r + "个小时前";
        }
        if (diff > minute) {
            r = (diff / minute);
            return r + "分钟前";
        }
        return "刚刚";
    }

    /**
     * 将日期以yyyy-MM-dd HH:mm:ss格式化
     *
     * @param dateL 日期
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String formatDateTime(long dateL) {
        SimpleDateFormat sdf = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
        Date date = new Date(dateL);
        return sdf.format(date);
    }

    /**
     * 将日期以yyyy-MM-dd HH:mm:ss格式化
     *
     * @param dateL 日期
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String formatDateTime(long dateL, String formater) {
        SimpleDateFormat sdf = new SimpleDateFormat(formater);
        return sdf.format(new Date(dateL));
    }


    /**
     * 将日期以yyyy-MM-dd HH:mm:ss格式化
     *
     * @param date     日期
     * @param formater
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String formatDateTime(Date date, String formater) {
        SimpleDateFormat sdf = new SimpleDateFormat(formater);
        return sdf.format(date);
    }

    /**
     * 将日期字符串转成日期
     *
     * @param strDate 字符串日期
     * @return java.util.date日期类型
     */
    @SuppressLint("SimpleDateFormat")
    public static Date parseDate(String strDate) {
        DateFormat dateFormat = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
        Date returnDate = null;
        try {
            returnDate = dateFormat.parse(strDate);
        } catch (ParseException e) {

        }
        return returnDate;

    }

    /**
     * 获取系统当前日期
     *
     * @return
     */
    public static Date gainCurrentDate() {
        return new Date();
    }

    /**
     * 验证日期是否比当前日期早
     *
     * @param target1 比较时间1
     * @param target2 比较时间2
     * @return true 则代表target1比target2晚或等于target2，否则比target2早
     */
    public static boolean compareDate(Date target1, Date target2) {
        boolean flag = false;
        try {
            String target1DateTime = formatDateTime(target1, DF_YYYY_MM_DD_HH_MM_SS);
            String target2DateTime = formatDateTime(target2, DF_YYYY_MM_DD_HH_MM_SS);
            if (target1DateTime.compareTo(target2DateTime) <= 0) {
                flag = true;
            }
        } catch (Exception e) {
            System.out.println("比较失败，原因：" + e.getMessage());
        }
        return flag;
    }

    /**
     * 对日期进行增加操作
     *
     * @param target 需要进行运算的日期
     * @param hour   小时
     * @return
     */
    public static Date addDateTime(Date target, double hour) {
        if (null == target || hour < 0) {
            return target;
        }

        return new Date(target.getTime() + (long) (hour * 60 * 60 * 1000));
    }

    /**
     * 对日期进行相减操作
     *
     * @param target 需要进行运算的日期
     * @param hour   小时
     * @return
     */
    public static Date subDateTime(Date target, double hour) {
        if (null == target || hour < 0) {
            return target;
        }

        return new Date(target.getTime() - (long) (hour * 60 * 60 * 1000));
    }

    /**
     * 获取系统时间的方法:月/日 时:分:秒
     */
    public static String getFormateDate() {
        Calendar calendar = Calendar.getInstance();
        int month = (calendar.get(Calendar.MONTH) + 1);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        String systemTime = (month < 10 ? "0" + month : month) + "/" + (day < 10 ? "0" + day : day) + "  " + (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second);
        return systemTime;
    }


    /**
     * 获取系统时间的方法:时
     */
    public static String getHour() {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        return ((hour < 10 ? "0" + hour : hour) + "");
    }

    /**
     * 将2017-07-10 00:00:00 换2017-07-10
     *
     * @param strDate
     * @return
     */
    public static String strFormatStr(String strDate) {
        if (strDate.equals("")) {
            return "";
        }
        return dateToStr(strToDate(strDate));
    }

    /**
     * 2015-01-07 15:05:34
     *
     * @param strDate
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static Date strToDateHHMMSS(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 2015-01-07
     *
     * @param strDate
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 2015.01.07
     *
     * @param strDate
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static Date strToDateDorp(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    @SuppressLint("SimpleDateFormat")
    public static String dateToStr(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * 传入一个String转化为long
     */
    @SuppressLint("SimpleDateFormat")
    public static Long stringParserLong(String param) throws ParseException {
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.parse(param).getTime();
    }

    /**
     * 当前时间转换为long
     */
    @SuppressLint("SimpleDateFormat")
    public static Long currentDateParserLong() throws ParseException {
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.parse(format.format(Calendar.getInstance().getTime())).getTime();
    }

    /**
     * 当前时间 如: 2013-04-22 10:37:00
     */
    @SuppressLint("SimpleDateFormat")
    public static String getCurrentTime() {
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(Calendar.getInstance().getTime());
    }

    /**
     * 当前时间 如: 10:37
     */
    @SuppressLint("SimpleDateFormat")
    public static String getCurrentDateHHMM() {
        format = new SimpleDateFormat("HH:mm");
        return format.format(Calendar.getInstance().getTime());
    }

    /**
     * 当前时间 如: 10:37
     *
     * @throws ParseException
     */
    @SuppressLint("SimpleDateFormat")
    public static String getCurrentDateHHMMSS() {
        format = new SimpleDateFormat("HH:mm:ss");
        return format.format(Calendar.getInstance().getTime());
    }

    /**
     * 当前时间 如: 20130422
     */
    @SuppressLint("SimpleDateFormat")
    public static String getCurrentDateString() {
        format = new SimpleDateFormat("yyyyMMddHHmm");
        return format.format(Calendar.getInstance().getTime());
    }

    /**
     * 当前时间 如: 2013-04-22
     */
    @SuppressLint("SimpleDateFormat")
    public static String getCurrentDate() {
        format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(Calendar.getInstance().getTime());
    }

    @SuppressLint("SimpleDateFormat")
    public static String getSWAHDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(Calendar.getInstance().getTime());
    }

    @SuppressLint("SimpleDateFormat")
    public static Long stringToLongD(String param) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.parse(param.substring(0, param.length() - 4)).getTime();
    }

    @SuppressLint("SimpleDateFormat")
    public static Long stringToLong(String param) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
        return format.parse(param).getTime();
    }

    /**
     * 获取两个日期之间的间隔天数
     *
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static int getGapCount(Date startDate, Date endDate) {
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(startDate);
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        fromCalendar.set(Calendar.MINUTE, 0);
        fromCalendar.set(Calendar.SECOND, 0);
        fromCalendar.set(Calendar.MILLISECOND, 0);

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(endDate);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);

        return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
    }

    /**
     * 日期转换成Java字符串
     *
     * @param date
     * @return str
     */
    @SuppressLint("SimpleDateFormat")
    public static String DateToStr(Date date) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = format.format(date);
        return str;
    }

    /**
     * 字符串转换成日期
     *
     * @param str
     * @return date
     */
    @SuppressLint("SimpleDateFormat")
    public static Date StrToDate(String str) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 字符串转换成日期
     *
     * @param str
     * @return date
     */
    @SuppressLint("SimpleDateFormat")
    public static Date StrToDateDrop(String str) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * @param time
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static long getLongTime(String time) {
        long ct = 0;
        try {
            format = new SimpleDateFormat("HH:mm:ss");
            ct = format.parse(time).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ct;
    }

    /**
     * 判断两日期是否同一天
     *
     * @param str1
     * @param str2
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static boolean isSameDay(String str1, String str2) {

        Date day1 = null, day2 = null;
        day1 = DateTimeUtil.strToDate(str1);
        day2 = DateTimeUtil.strToDate(str2);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        String ds1 = sdf.format(day1);

        String ds2 = sdf.format(day2);

        if (ds1.equals(ds2)) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 获取两个日期的时间差
     */
    @SuppressLint("SimpleDateFormat")
    public static int getTimeInterval(String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int interval = 0;
        try {
            Date currentTime = new Date();// 获取现在的时间
            Date beginTime = dateFormat.parse(date);
            interval = (int) ((beginTime.getTime() - currentTime.getTime()) / (1000));// 时间差
            // 单位秒
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return interval;
    }

    /**
     * 获取两个日期的时间差 yyyy.MM.dd HH.mm.ss
     */
    @SuppressLint("SimpleDateFormat")
    public static int getInterval(String bDate, String eDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
        int interval = 0;
        try {
            Date currentTime = dateFormat.parse(eDate);// 获取现在的时间
            Date beginTime = dateFormat.parse(bDate);
            interval = (int) ((beginTime.getTime() - currentTime.getTime()));// 时间差
            // 单位秒
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return interval;
    }

    /**
     * 两个时间之差 求出一个long Time
     *
     * @param date
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static long getTime(String date) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long diff = 0;
        try {
            Date currentTime = new Date();// 获取现在的时间
            Date getdate = df.parse(date);
            diff = getdate.getTime() - currentTime.getTime();

        } catch (Exception e) {
        }
        return diff;
    }


    /**
     * 日期转换成Java字符串
     *
     * @param DATE1
     * @param DATE2
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static int compare_date(String DATE1, String DATE2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() >= dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    /**
     * 传入时间 算出星期几
     *
     * @param str  2014年1月3日
     * @param days 1:2014年1月4日 类推
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String formatDate(String str, int days) {

        String dateStr = "";
        try {
            DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
            Date date = df.parse(str);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            Date d = dateFormat.parse(dateFormat.format(date));
            c.setTime(d);
            c.add(Calendar.DAY_OF_MONTH, days);
            switch (c.get(Calendar.DAY_OF_WEEK) - 1) {
                case 0:
                    dateStr = "周日";
                    break;
                case 1:
                    dateStr = "周一";
                    break;
                case 2:
                    dateStr = "周二";
                    break;
                case 3:
                    dateStr = "周三";
                    break;
                case 4:
                    dateStr = "周四";
                    break;
                case 5:
                    dateStr = "周五";
                    break;
                case 6:
                    dateStr = "周六";
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return dateStr;
    }


    /**
     * 判断当前日期是星期几
     *
     * @param pTime 修要判断的时间
     * @return dayForWeek 判断结果
     * @Exception 发生异常
     */

    public static int dayForWeekNUM(String pTime) throws Exception {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Calendar c = Calendar.getInstance();

        c.setTime(format.parse(pTime));

        int dayForWeek = 0;

        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {

            dayForWeek = 7;
        } else {

            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return dayForWeek;

    }

    public final static String FORMAT_DATE = "yyyy-MM-dd";
    public final static String FORMAT_TIME = "HH:mm:ss";
    public final static String FORMAT_YEAR_TIME = "yy:MM:dd:HH:mm:ss";
    public final static String FORMAT_TIME_MILLS = "HH:mm:ss:SS";
    public final static String FORMAT_DATE_TIME = "yyyy-MM-dd hh:mm:ss";
    public final static String FORMAT_MONTH_DAY_TIME = "MM月dd日 hh:mm";
    private static SimpleDateFormat sdf = new SimpleDateFormat();
    private static final int YEAR = 365 * 24 * 60 * 60;// 年
    private static final int MONTH = 30 * 24 * 60 * 60;// 月
    private static final int DAY = 24 * 60 * 60;// 天
    private static final int HOUR = 60 * 60;// 小时
    private static final int MINUTE = 60;// 分钟

    /**
     * 获取当前时间的byte数组
     * 格式为 年（后两位）:月:日:时:分:秒
     *
     * @return 时间数组
     */
    public static synchronized byte[] getCurrentTimeDetail() {
        long curTimeMillis = System.currentTimeMillis();
        String time = getFormatTimeFromTimestamp(curTimeMillis, FORMAT_YEAR_TIME);
        String[] timeStrArray = time.split(":");
        byte[] timeArray = new byte[6];
        for (int x = 0; x < timeStrArray.length; x++) {
            timeArray[x] = Byte.parseByte(timeStrArray[x]);
        }
        return timeArray;
    }

    /**
     * 根据时间戳获取描述性时间，如3分钟前，1天前
     *
     * @param timestamp 时间戳 单位为毫秒
     * @return 时间字符串
     */
    public static String getDescriptionTimeFromTimestamp(long timestamp) {
        long currentTime = System.currentTimeMillis();
        long timeGap = (currentTime - timestamp) / 1000;// 与现在时间相差秒数
        String timeStr = null;
        if (timeGap > YEAR) {
            timeStr = timeGap / YEAR + "年前";
        } else if (timeGap > MONTH) {
            timeStr = timeGap / MONTH + "个月前";
        } else if (timeGap > DAY) {// 1天以上
            timeStr = timeGap / DAY + "天前";
        } else if (timeGap > HOUR) {// 1小时-24小时
            timeStr = timeGap / HOUR + "小时前";
        } else if (timeGap > MINUTE) {// 1分钟-59分钟
            timeStr = timeGap / MINUTE + "分钟前";
        } else {// 1秒钟-59秒钟
            timeStr = "刚刚";
        }
        return timeStr;
    }

    /**
     * 根据时间戳获取指定格式的时间，如2011-11-30 08:40
     *
     * @param timestamp 时间戳 单位为毫秒
     * @param format    指定格式 如果为null或空串则使用默认格式"yyyy-MM-dd HH:MM"
     * @return
     */
    public static synchronized String getFormatTimeFromTimestamp(long timestamp, String format) {
        sdf.applyPattern(format);
        Date date = new Date(timestamp);
        return sdf.format(date);
    }

    /**
     * 根据时间戳获取时间字符串，并根据指定的时间分割数partionSeconds来自动判断返回描述性时间还是指定格式的时间
     *
     * @param timestamp      时间戳 单位是毫秒
     * @param partionSeconds 时间分割线，当现在时间与指定的时间戳的秒数差大于这个分割线时则返回指定格式时间，否则返回描述性时间
     * @param format
     * @return
     */
    public static String getMixTimeFromTimestamp(long timestamp, long partionSeconds, String format) {
        long currentTime = System.currentTimeMillis();
        long timeGap = (currentTime - timestamp) / 1000;// 与现在时间相差秒数
        if (timeGap <= partionSeconds) {
            return getDescriptionTimeFromTimestamp(timestamp);
        } else {
            return getFormatTimeFromTimestamp(timestamp, format);
        }
    }

    /**
     * 获取当前日期的指定格式的字符串
     *
     * @param format 指定的日期时间格式，若为null或""则使用指定的格式"yyyy-MM-dd HH:MM"
     * @return
     */
    public static String getCurrentTime(String format) {
        if (format == null || format.trim().equals("")) {
            sdf.applyPattern(FORMAT_DATE_TIME);
        } else {
            sdf.applyPattern(format);
        }
        return sdf.format(new Date());
    }

    /**
     * 将日期字符串以指定格式转换为Date
     *
     * @param timeStr 日期字符串
     * @param format  指定的日期格式，若为null或""则使用指定的格式"yyyy-MM-dd HH:MM"
     * @return
     */
    public static Date getTimeFromString(String timeStr, String format) {
        if (format == null || format.trim().equals("")) {
            sdf.applyPattern(FORMAT_DATE_TIME);
        } else {
            sdf.applyPattern(format);
        }
        try {
            return sdf.parse(timeStr);
        } catch (ParseException e) {
            return new Date();
        }
    }

    /**
     * 将Date以指定格式转换为日期时间字符串
     *
     * @param date   日期
     * @param format 指定的日期时间格式，若为null或""则使用指定的格式"yyyy-MM-dd HH:MM"
     * @return
     */
    public static String getStringFromTime(Date date, String format) {
        if (format == null || format.trim().equals("")) {
            sdf.applyPattern(FORMAT_DATE_TIME);
        } else {
            sdf.applyPattern(format);
        }
        return sdf.format(date);
    }

    /**
     * 获取系统当前时间
     *
     * @return
     */
    public static String getSystemDataTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    public static String getSystemDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String str = formatter.format(new Date(System.currentTimeMillis()));
            return str;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * @param date1
     * @param date2
     * @return
     */
    public static int timeCompare(String date1, String date2) {
        if (TextUtils.isEmpty(date1) || TextUtils.isEmpty(date2)) {
            return 0;
        }
        //格式化时间
        SimpleDateFormat CurrentTime = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date beginTime = CurrentTime.parse(date1);
            Date endTime = CurrentTime.parse(date2);
            if (beginTime.before(endTime)) {
                return 1;
            }
            if (beginTime.after(endTime)) {
                return -1;
            }
            return 0;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static final String PATTERN_DEFAULT = "yyyy-MM-dd";
    /*
     * 本周
     */
    public static final int TYPE_CUR_WEEK = 0;
    /*
     * 本月
     */
    public static final int TYPE_CUR_MONTH = 1;
    /*
     * 上个月
     */
    public static final int TYPE_LAST_MONTH = 2;
    /*
     * 当前季度
     */
    public static final int TYPE_CUR_QUARTER = 3;
    /*
     * 上个季度
     */
    public static final int TYPE_LAST_QUARTER = 4;
    /*
     * 上半年
     */
    public static final int TYPE_THE_1ST_HALF_OF_YEAR = 5;
    /*
     * 下半年
     */
    public static final int TYPE_THE_2ND_HALF_OF_YEAR = 6;

    public static void main(String[] args) {
        String curDateStr = "2016-02-21";
        // String [] result = getBeginAndEndDateArray(
        // curDateStr,PATTERN_DEFAULT, TYPE_THE_2ND_HALF_OF_YEAR);
        // String [] result = getBeginAndEndDateArray(
        // curDateStr,PATTERN_DEFAULT, TYPE_LAST_QUARTER);
        // String [] result = getBeginAndEndDateArray(
        // curDateStr,PATTERN_DEFAULT, TYPE_CUR_QUARTER);
        // String [] result = getBeginAndEndDateArray(
        // curDateStr,PATTERN_DEFAULT, TYPE_CUR_MONTH);
        // String [] result = getBeginAndEndDateArray(
        // curDateStr,PATTERN_DEFAULT, TYPE_LAST_MONTH);
        // String [] result = getBeginAndEndDateArray(
        // curDateStr,PATTERN_DEFAULT, TYPE_CUR_WEEK);
        String[] result = getBeginAndEndDateArray(curDateStr, PATTERN_DEFAULT,
                TYPE_CUR_WEEK);
        System.out.println("1--------------------------" + result[0]);
        System.out.println("2--------------------------" + result[1]);
    }

    /**
     * 根据传入的类型和日期获取开始日期和结束日期的数组
     *
     * @param dateStr 目标日期 示例2016-02-17
     * @param pattern 使用的日期模板，必须和第一个参数保持一致
     * @param type    获取类型，详见TYPE_XXX常量
     * @return 由开始和结束日期组成的String数组
     */
    public static String[] getBeginAndEndDateArray(String dateStr,
                                                   String pattern, int type) {
        String[] result = null;
        Date date = strToDate(dateStr, pattern);
        switch (type) {
            case TYPE_CUR_WEEK:
                result = getWeekBegin2End(date, PATTERN_DEFAULT);
                break;
            case TYPE_CUR_MONTH:
                result = getMonthBegin2End(date, PATTERN_DEFAULT);
                break;
            case TYPE_LAST_MONTH:
                Date lastMonthDate = getDateOfLastMonth(date);
                result = getMonthBegin2End(lastMonthDate, PATTERN_DEFAULT);
                break;
            case TYPE_CUR_QUARTER:
                result = getQuarterBegin2End(date, PATTERN_DEFAULT);
                break;
            case TYPE_LAST_QUARTER:
                Date lastQuarterDate = getDateOfLastQuarter(date);
                result = getQuarterBegin2End(lastQuarterDate, PATTERN_DEFAULT);
                break;
            case TYPE_THE_1ST_HALF_OF_YEAR:
                result = get1stHalfYearDate(date, PATTERN_DEFAULT);
                break;
            case TYPE_THE_2ND_HALF_OF_YEAR:
                result = get2ndHalfYearDate(date, PATTERN_DEFAULT);
                break;
            default:
                break;
        }
        return result;
    }

    /**
     * 获取从指定日期前N天到该日期的字符串数组
     *
     * @param dayCountBefore 前n天
     * @param dateStr        指定日期字符串
     * @param pattern        指定日期模板，必须与第二个参数一致。
     * @return 指定字符串数组
     * @throws NullPointerException
     */
    public static String[] getDayBegin2End(int dayCountBefore, String dateStr,
                                           String pattern) throws NullPointerException {
        Date date = strToDate(dateStr, pattern);
        String endDateStr = date2String(date, pattern);
        Date beginDate = getPreviewDay(date, dayCountBefore);
        String beginDateStr = date2String(beginDate, pattern);
        return new String[]{beginDateStr, endDateStr};
    }

    private static String[] getWeekBegin2End(Date date, String pattern) {
        String mondayStr = getWeekFirstDateStr(date, pattern);
        String sundayStr = getWeekLastDateStr(date, pattern);
        return new String[]{mondayStr, sundayStr};
    }

    private static boolean isSunday(Calendar c) {
        return c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
    }

    private static String getWeekFirstDateStr(Date date, String pattern) {
        if (date != null) {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            if (isSunday(c)) {
                c.set(Calendar.WEEK_OF_YEAR, c.get(Calendar.WEEK_OF_YEAR) - 1);
                c.set(Calendar.DAY_OF_WEEK,
                        c.getActualMinimum(Calendar.DAY_OF_WEEK) + 1);
                date = c.getTime();
                date.setDate(date.getDate() - 7);
            } else {
                c.set(Calendar.DAY_OF_WEEK,
                        c.getActualMinimum(Calendar.DAY_OF_WEEK) + 1);
                date = c.getTime();
            }
        }
        return date2String(date, pattern);
    }

    private static String getWeekLastDateStr(Date date, String pattern) {
        if (date != null) {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            if (isSunday(c)) {
                c.set(Calendar.WEEK_OF_YEAR, c.get(Calendar.WEEK_OF_YEAR));
                c.set(Calendar.DAY_OF_WEEK,
                        c.getActualMinimum(Calendar.DAY_OF_WEEK));
                date = c.getTime();
            } else {
                c.set(Calendar.WEEK_OF_YEAR, c.get(Calendar.WEEK_OF_YEAR) + 1);
                c.set(Calendar.DAY_OF_WEEK,
                        c.getActualMinimum(Calendar.DAY_OF_WEEK));
                date = c.getTime();
                date.setDate(date.getDate() + 7);
            }
        }
        return date2String(date, pattern);
    }

    /**
     * 获取前n天的Date对象
     *
     * @param date
     * @param n    前几天
     * @return
     */
    private static Date getPreviewDay(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -n);
        date = calendar.getTime();
        return date;
    }

    /**
     * 当前向前推几天
     *
     * @param n
     * @return
     */
    public static String getPreviewDate(int n) {
        Date dateP = getPreviewDay(new Date(), n);
        SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
        return matter1.format(dateP);
    }

    private static String[] get1stHalfYearDate(Date date, String pattern) {
        Date beginDate = getFirstDateOfYear(date);
        Date endDate = getLastDateOf1stYearHalf(date);
        String beginDateStr = date2String(beginDate, pattern);
        String endDateStr = date2String(endDate, pattern);
        return new String[]{beginDateStr, endDateStr};
    }

    private static String[] get2ndHalfYearDate(Date date, String pattern) {
        Date beginDate = getFirstDateOf2ndYearHalf(date);
        Date endDate = getLastDateOfYear(date);
        String beginDateStr = date2String(beginDate, pattern);
        String endDateStr = date2String(endDate, pattern);
        return new String[]{beginDateStr, endDateStr};
    }

    /**
     * 获取季度开始和结束天
     *
     * @param date
     * @param pattern
     * @return
     */
    private static String[] getQuarterBegin2End(Date date, String pattern) {
        String beginDay = getQuarterBeginDayStr(date, pattern);
        String endDay = getQuarterEndDayStr(date, pattern);
        return new String[]{beginDay, endDay};
    }

    private static String[] getMonthBegin2End(Date date, String pattern) {
        String beginDay = getMonthBeginDayStr(date, pattern);
        String endDay = getMonthEndDayStr(date, pattern);
        return new String[]{beginDay, endDay};
    }

    public static String[] getMonthBeginToEnd() {
        String beginDay = getMonthBeginDayStr(new Date(), "yyyy-MM-dd");
        String endDay = getMonthEndDayStr(new Date(), "yyyy-MM-dd");
        return new String[]{beginDay, endDay};
    }

    /**
     * 获取指定季度第一天的字符串
     *
     * @param date
     * @param pattern 输出日期的模板
     * @return
     */
    private static String getQuarterBeginDayStr(Date date, String pattern) {
        Date resultDate = getFirstDateOfQuarter(date);
        return date2String(resultDate, pattern);
    }

    private static String getQuarterEndDayStr(Date date, String pattern) {
        Date resultDate = getLastDateOfQuarter(date);
        return date2String(resultDate, pattern);
    }

    private static String getMonthBeginDayStr(Date date, String pattern) {
        Date resultDate = getFirstDateOfMonth(date);
        return date2String(resultDate, pattern);
    }

    private static String getMonthEndDayStr(Date date, String pattern) {
        Date resultDate = getLastDateOfMonth(date);
        return date2String(resultDate, pattern);
    }

    private static String date2String(Date date, String pattern)
            throws NullPointerException {
        return (new SimpleDateFormat(pattern)).format(date);
    }

    /**
     * 取得季度第一天
     *
     * @param date
     * @return
     */
    private static Date getFirstDateOfQuarter(Date date) {
        return getFirstDateOfMonth(getQuarterDate(date)[0]);
    }

    /**
     * 取得季度最后一天
     *
     * @param date
     * @return
     */
    private static Date getLastDateOfQuarter(Date date) {
        return getLastDateOfMonth(getQuarterDate(date)[2]);
    }

    /**
     * 取得月第一天
     *
     * @param date
     * @return
     */
    private static Date getFirstDateOfMonth(Date date) {
        if (date != null) {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.set(Calendar.DAY_OF_MONTH,
                    c.getActualMinimum(Calendar.DAY_OF_MONTH));
            date = c.getTime();
        }
        return date;
    }

    /**
     * 取得月最后一天
     *
     * @param date
     * @return
     */
    private static Date getLastDateOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    /**
     * 取得年第一天
     *
     * @param date
     * @return
     */
    private static Date getFirstDateOfYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_YEAR, c.getActualMinimum(Calendar.DAY_OF_YEAR));
        return c.getTime();
    }


    /**
     * 取得上半年最后一天
     *
     * @param date
     * @return
     */
    private static Date getLastDateOf1stYearHalf(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.MONTH, Calendar.JUNE);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    /**
     * 取得下半年第一天
     *
     * @param date
     * @return
     */
    private static Date getFirstDateOf2ndYearHalf(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.MONTH, Calendar.JULY);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    /**
     * 取得年最后一天
     *
     * @param date
     * @return
     */
    private static Date getLastDateOfYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_YEAR, c.getActualMaximum(Calendar.DAY_OF_YEAR));
        return c.getTime();
    }


    /**
     * 字符串转换成日期
     *
     * @param str
     * @return date
     */
    public static Date strToDate(String str, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取上个季度的同一天Date对象
     *
     * @param date
     * @return
     */
    private static Date getDateOfLastQuarter(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -3);
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取上个月的同一天Date对象
     *
     * @param date
     * @return
     */
    public static Date getDateOfLastMonth(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, -1);
            date = calendar.getTime();
        }
        return date;
    }

    /**
     * 取得季度月
     *
     * @param date
     * @return
     */
    private static Date[] getQuarterDate(Date date) {
        Date[] season = new Date[3];

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int nSeason = getQuarter(date);
        if (nSeason == 1) {// 第一季度
            c.set(Calendar.MONTH, Calendar.JANUARY);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.FEBRUARY);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.MARCH);
            season[2] = c.getTime();
        } else if (nSeason == 2) {// 第二季度
            c.set(Calendar.MONTH, Calendar.APRIL);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.MAY);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.JUNE);
            season[2] = c.getTime();
        } else if (nSeason == 3) {// 第三季度
            c.set(Calendar.MONTH, Calendar.JULY);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.AUGUST);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.SEPTEMBER);
            season[2] = c.getTime();
        } else if (nSeason == 4) {// 第四季度
            c.set(Calendar.MONTH, Calendar.OCTOBER);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.NOVEMBER);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.DECEMBER);
            season[2] = c.getTime();
        }
        return season;
    }

    /**
     * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
     *
     * @param date
     * @return
     */
    private static int getQuarter(Date date) {

        int season = 0;

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(Calendar.MONTH);
        switch (month) {
            case Calendar.JANUARY:
            case Calendar.FEBRUARY:
            case Calendar.MARCH:
                season = 1;
                break;
            case Calendar.APRIL:
            case Calendar.MAY:
            case Calendar.JUNE:
                season = 2;
                break;
            case Calendar.JULY:
            case Calendar.AUGUST:
            case Calendar.SEPTEMBER:
                season = 3;
                break;
            case Calendar.OCTOBER:
            case Calendar.NOVEMBER:
            case Calendar.DECEMBER:
                season = 4;
                break;
            default:
                break;
        }
        return season;
    }

    /**
     * 通过年份和月份 得到当月的日子
     *
     * @param year
     * @param month
     * @return
     */
    public static int getMonthDays(int year, int month) {
        month++;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                    return 29;
                } else {
                    return 28;
                }
            default:
                return -1;
        }
    }

    /**
     * 返回当前月份1号位于周几
     *
     * @param year  年份
     * @param month 月份，传入系统获取的，不需要正常的
     * @return 日：1 一：2 二：3 三：4 四：5 五：6 六：7
     */
    public static int getFirstDayWeek(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        Log.d("DateView", "DateView:First:" + calendar.getFirstDayOfWeek());
        return calendar.get(Calendar.DAY_OF_WEEK);
    }


    /**
     * 通过日期获取星期几
     *
     * @param pTime
     * @return
     */
    public static String dayForWeek(String pTime) {
        int value = 0;
        try {
            value = dayForWeekNUM(pTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (value == 1) {
            return "星期一";
        } else if (value == 2) {
            return "星期二";
        } else if (value == 3) {
            return "星期三";
        } else if (value == 4) {
            return "星期四";
        } else if (value == 5) {
            return "星期五";
        } else if (value == 6) {
            return "星期六";
        } else if (value == 7) {

            return "星期天";
        } else {
            return "";

        }
    }

    /**
     * 把日期中的T替换为空格 replace MyToast 替换  “”
     */

    public static String replaceDateT(String date) {
        return date.replaceAll("T", " ");
    }


    /**
     * 将日期转化为毫秒数
     *
     * @param date
     * @param format
     * @return
     */
    public static long setMilliseconds(String date, String format) {
        // 时间转化"yyyy-MM-dd HH:mm"
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        // 捕获异常
        try {
            // 转化开始时间毫秒数
            long result = dateFormat.parse(date).getTime();
            // 返回值
            return result;
        } catch (ParseException e) {
            // 异常打印
            e.printStackTrace();
            return 0;
        }

    }


    public static String getSystemDataTime(String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    public static Date getCurrentSystemDate(String serviceTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = formatter.parse(serviceTime);
            return date;
        } catch (Exception e) {
            Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
            return curDate;
        }
    }

    /**
     * 将时间戳转换为对应格式的日期
     *
     * @param pattern
     * @param millis
     * @return
     */
    public static String getDataTime(String pattern, long millis) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        Date curDate = new Date(millis);// 获取当前时间
        String str = formatter.format(curDate);
        return str;
    }


    public static String getSystemSplitTime(String serviceTime) {
        try {
            String[] ts = serviceTime.split("T");
            String[] split = ts[1].split(":");
            return split[0] + ":" + split[1];
        } catch (Exception e) {
            return "";
        }

    }

    public static String getSystemSplitDate(String serviceTime) {
        try {
            String[] ts = serviceTime.split("T");
            return ts[0];
        } catch (Exception e) {
            e.printStackTrace();
            return serviceTime;
        }
    }


    /**
     * 1 前者大于后者     -1
     *
     * @param begin
     * @param end
     * @return
     */
    public static int dateTimeCompare(String begin, String end) {
        if (TextUtils.isEmpty(begin) || TextUtils.isEmpty(end)) {
            return -1;
        }
        //格式化时间
        begin = begin.replace("MyToast", " ");
        end = end.replace("MyToast", " ");
        SimpleDateFormat currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date beginTime = currentTime.parse(begin);
            Date endTime = currentTime.parse(end);
            if ((endTime.getTime() - beginTime.getTime()) > 0) {
                return 1;
            } else if ((endTime.getTime() - beginTime.getTime()) == 0) {
                return 0;
            } else {
                return -1;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


    public static String getSystemDate(String serviceTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = formatter.parse(serviceTime);
            String str = formatter.format(date);
            return str;
        } catch (Exception e) {
            Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
            String str = formatter.format(curDate);
            return str;
        }
    }

    public static String getTodayDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date();
        String str = formatter.format(curDate);
        return str;
    }

    public static String getTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date();
        String str = formatter.format(curDate);
        return str;
    }

    /**
     * 昨天
     */
    public static String getYesterdayDate() {
        SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
        Date as = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
        String time = matter1.format(as);
        return time;
    }

    /**
     * @param days   天数
     * @param isNext true未来 false 过去
     * @return
     */
    public static String getLimitDate(int days, boolean isNext) {
        SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
        Date as;
        if (isNext) {
            as = new Date(System.currentTimeMillis() + days * 24 * 60 * 60 * 1000);
        } else {
            as = new Date(System.currentTimeMillis() - days * 24 * 60 * 60 * 1000);
        }

        String time = matter1.format(as);
        return time;
    }

    public static String getSystemTime(String serviceTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(serviceTime.replace("MyToast", " "));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int minute = calendar.get(Calendar.MINUTE);
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            if (minute < 10) {
                if (hour < 10) {
                    return "0" + calendar.get(Calendar.HOUR_OF_DAY) + ":" + "0" + calendar.get(Calendar.MINUTE);
                }
                return calendar.get(Calendar.HOUR_OF_DAY) + ":" + "0" + calendar.get(Calendar.MINUTE);
            } else {
                if (hour < 10) {
                    return "0" + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE);
                }
                return calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE);
            }
            //	java.sql.Timestamp time = new java.sql.Timestamp(new java.util.Date().getTime());
            //	return time.getHours()+time.getMinutes();
        } catch (Exception e) {
            Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(curDate);
            return calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE);
        }
    }

    // 获取上周第一天
    public static String getLastWeekFirstDay() {
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, 1);
        Date data = cal.getTime();
        Date as = new Date(data.getTime() - 7 * 24 * 60 * 60 * 1000);
        return dateFormater.format(as);
    }

    // 获取上周最后一天
    public static String getLastWeekLastDay() {
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        Date data = cal.getTime();
        Date as = new Date(data.getTime() - 7 * 24 * 60 * 60 * 1000);
        return dateFormater.format(as);
    }

    // 获取本周第一天
    public static String getDayOfWeekFirstDay() {
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, 1);
        cal.getTime();
        return dateFormater.format(cal.getTime()) + "";
    }

    // 获取本周最后一天
    public static String getDayOfWeekLastDay() {
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.getTime();
        return dateFormater.format(cal.getTime()) + "";
    }

    // 获取本月的第一天
    public static String getDayOfMonthFirstDay() {
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.getTime();
        return dateFormater.format(cal.getTime()) + "";
    }

    // 获取本月的最后一天
    public static String getDayOfMonthLastDaya() {
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.getTime();
        return dateFormater.format(cal.getTime()) + "";
    }

    //获取当前季度的开始时间
    public static String getDayOfQuarterFirstDay() {
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 6);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            c.getTime();
            return dateFormater.format(c.getTime()) + "";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //获取当前季度的最后一天时间
    public static String getDayOfQuarterLastDay() {
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            c.getTime();
            return dateFormater.format(c.getTime()) + "";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

//	public static String getSystemDate(String str){
//		try{
//			String[] ts = str.split("MyToast");
//			return ts[0] ;
//		}catch (Exception e){
//			e.printStackTrace();
//			return "" ;
//		}
//	}
//
//	public static String getSystemTime(String str){
//		try{
//		//	String serviceTime = App.getInstance().serviceTime;
//			String[] ts = str.split("MyToast");
//			return ts[1] ;
//		}catch (Exception e){
//			e.printStackTrace();
//			return "" ;
//		}
//	}


    public static String getNowDate() {
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(dt);
    }

    public static int getWeekOfYear(String today) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = format.parse(today);
            Calendar calendar = Calendar.getInstance();
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
            calendar.setTime(date);
            return calendar.get(Calendar.WEEK_OF_YEAR);
        } catch (Exception e) {
            e.printStackTrace();
            return 1;
        }

    }

    public static String getMonthOfYear(String today) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = format.parse(today);
            Calendar calendar = Calendar.getInstance();
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
            calendar.setTime(date);
            return String.valueOf(calendar.get(Calendar.MONTH) + 1);
        } catch (Exception e) {
            e.printStackTrace();
            return "1";
        }

    }

    //获取当前日期时分
    public static String getHourAndMinute() {
        Calendar calendar = Calendar.getInstance();
        int minute = calendar.get(Calendar.MINUTE);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (minute < 10) {
            if (hour < 10) {
                return "0" + calendar.get(Calendar.HOUR_OF_DAY) + ":" + "0" + calendar.get(Calendar.MINUTE);
            }
            return calendar.get(Calendar.HOUR_OF_DAY) + ":" + "0" + calendar.get(Calendar.MINUTE);
        } else {
            if (hour < 10) {
                return "0" + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE);
            }
            return calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE);
        }
    }

    //获得yyyy-MM-dd HH:mm格式日期
    public static String getDateMin() {
        //设置时间格式
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //获取当前毫秒数
        String date1 = format1.format(new Date(System.currentTimeMillis()));
        //返回日期字符串
        return date1;// 2012-10-03 23:41:31
    }


    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static String getYearFirst(int year, String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        SimpleDateFormat f = new SimpleDateFormat(pattern);
        String sDate = f.format(currYearFirst);
        return sDate;
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static String getYearLast(int year, String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        SimpleDateFormat f = new SimpleDateFormat(pattern);
        String sDate = f.format(currYearLast);
        return sDate;
    }

    public static String[] getYear1st2nd() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        String st = getYearFirst(year, "yyyy-MM-dd");
        String nd = getYearLast(year, "yyyy-MM-dd");
        return new String[]{st, nd};
    }

    /**
     * 获取当年的第一天
     */
    public static String getCurrYearFirst(String pattern) {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearFirst(currentYear, pattern);
    }
}
